<?php

/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */
namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Db\TableGateway\TableGateway;

class ClientController extends AbstractActionController {
	private $clientTable;
	public function indexAction() {
		$columnList = array (
				"選択",
				"ID",
				"氏名",
				"所属施設",
				"年齢",
				"介護度",
				"支援機関" 
		);
		$statement = $this->getServiceLocator ()->get ( 'Zend\Db\Adapter\Adapter' )->query ( "
				select NULL AS $columnList[0],a.CLIENT_ID AS $columnList[1],
				a.ClientName AS $columnList[2],b.remarks AS $columnList[3],
				(YEAR(CURDATE()) - YEAR(a.birthday)) - (RIGHT(CURDATE(), 5) < RIGHT(a.birthday, 5)) AS $columnList[4],
				a.nursing_care_level AS $columnList[5],c.FACILITY_NAME AS $columnList[6]  
				from viw_client_ass a
				left join tbl_organization_group b on SUBSTRING(a.HCS_ID,1,4) = b.GROUPNAME
				left join tbl_support_facilities c on a.HCS_ID = c.HCS_ID
				where a.ClientName is not null
				group by a.CLIENT_ID
				" );
		$resultSet = $statement->execute ();
		$rowSet = array ();
		foreach ( $resultSet as $result ) {
			foreach ( $columnList as $column ) {
				$rowSet [$result ['ID']] [$column] = $result [$column];
			}
		}
		return new ViewModel ( array (
				'rowset' => $rowSet 
		) );
	}
	public function getClientTable() {
		if (! $this->clientTable) {
			$this->clientTable = new TableGateway ( 'tbl_client_basic', $this->getServiceLocator ()->get ( 'Zend\Db\Adapter\Adapter' ) );
		}
		return $this->clientTable;
	}
}
